<style scoped>
</style>

<template>

  <div>

    <Modal
      v-model="IsModalShow"
      :title="modalFormTitle"
      :mask-closable="false"
      @on-cancel="cancel"
      width="600"
    >
      <Form
        ref="modalForm"
        :model="modalForm"
        :label-width="80"
        value=true
        style="padding: 3px 60px"
      >
        <Form-item
          label="单位名称"
          prop="maintName"
          :rules="{required: true, message: '必填,1-50位字符',min:1,max:100, trigger:'blur',type:'string'}"
        >
          <Input v-model="modalForm.maintName"></Input>
        </Form-item>
        <Form-item
          label="责任人"
          prop="managerName"
          :rules="{required: true, message: '必填,1-50位字符',min:1,max:100, trigger:'blur',type:'string'}"
        >
          <Input v-model="modalForm.managerName"></Input>
        </Form-item>
        <Form-item
          label="联系电话"
          prop="managerMobile"
          :rules="{required: true, message: '输入11位手机号', trigger:'blur',type:'string',pattern: /^1\d{10}$/}"
        >
          <Input v-model="modalForm.managerMobile"></Input>
        </Form-item>
        <Form-item
          label="办公室电话"
          prop="officePhone"
        >
          <Input v-model="modalForm.officePhone"></Input>
        </Form-item>
        <Form-item
          label="所属部门"
          prop="cascader"
          :rules="{required: true, message: '必选', trigger:'change', type:'array'}"
        >
          <Cascader
            :data="deptCascader"
            v-model="modalForm.cascader"
            :filterable="true"
            change-on-select
            transfer
            placeholder="选择部门"
          ></Cascader>
        </Form-item>
        <Form-item
          label="单位地址"
          prop="maintAddr"
        >
          <Input
            v-model="modalForm.maintAddr"
            type="textarea"
          ></Input>
        </Form-item>
        <Form-item
          label="备注"
          prop="remark"
        >
          <Input
            v-model="modalForm.remark"
            type="textarea"
          ></Input>
        </Form-item>
      </Form>
      <div slot="footer">
        <Button
          type="default"
          @click="cancel"
        >关闭</Button>
        <Button
          type="primary"
          :loading="modalForm_loading"
          @click="saveForm('modalForm')"
        >保存
        </Button>
      </div>
    </Modal>
  </div>

</template>

<script>
import { saveMaintenance, updateMaintenance } from "@/api/asset";
import { getDeptCascader } from "@/api/system";

export default {
  props: {
    parentForm: {
      type: Object,
      default: function() {
        return {
          maintId: "",
          maintName: "",
          maintAddr: "",
          managerName: "",
          managerMobile: "",
          officePhone: "",
          remark: "",
          deptId: 1
        };
      }
    },
    modalShow: {
      type: Boolean,
      default: true
    },
    modalFormTitle: {
      type: String,
      default: "添加维保单位"
    }
  },
  data() {
    return {
      IsModalShow: false,
      modalForm: {},
      modalForm_loading: false,
      deptCascader: []
    };
  },
  watch: {
    modalShow(curVal, oldVal) {
      this.IsModalShow = curVal;
      if (curVal) {
        this.modalForm = JSON.parse(JSON.stringify(this.parentForm));
        if (this.parentForm.cascader) {
          this.modalForm.cascader = this.parentForm.cascader
            .split(",")
            .map(function(item) {
              return parseInt(item);
            });
        } else {
          this.modalForm.cascader = [];
        }
      }
    }
  },
  created() {
    this.getDeptCascader();
  },
  mounted() {},
  methods: {
    async getDeptCascader() {
      let res = await getDeptCascader();
      if (res.code == 0) {
        this.deptCascader = res.data;
      }
    },
    cancel() {
      this.$emit("listenModalForm");
    },
    saveForm(name) {
      this.$refs[name].validate(async valid => {
        if (valid) {
          this.modalForm_loading = true;
          let params = JSON.parse(JSON.stringify(this.modalForm));
          let arr = this.modalForm.cascader;
          params.deptId=arr[arr.length-1]
          params.cascader = arr.join(",");
          try {
            let result;
            if (params.maintId) {
              result = await updateMaintenance(params);
            } else {
              result = await saveMaintenance(params);
            }
            if (result.code == 0) {
              this.$emit("listenModalForm");
              this.$emit("refreshTableList");
            }
          } catch (err) {
            console.log(err);
            this.$Message.error("服务器异常，稍后再试");
          }
          this.modalForm_loading = false;
        } else {
          this.$Message.error("表单验证失败!");
        }
      });
    }
  }
};
</script>
